package com.cqw.commontest.utils;

import org.apache.commons.lang3.StringUtils;

import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.Map;

/**
 * @Author: caoqingwen
 * @CreateTime: 2021-04-08 10:21
 * @Description: httpClient调用
 */
public class HttpUtil {


    /**
     * 向指定url发送get请求
     *
     * @param url   请求的url
     * @param param 请求参数：格式：name1=value&name2=value2
     * @return 远程资源的响应结果
     */
    public static String sendGet(String url, String param) {
        String result = "";
        String urlNameString = "";
        if (StringUtils.isBlank(param)) {
            urlNameString = url;
        } else {
            urlNameString = url + "?" + param;
        }

        BufferedReader in = null;

        try {
            URL realUrl = new URL(urlNameString);
            //打开和url之间的链接
            URLConnection connection = realUrl.openConnection();
            //设置通用的请求属性
            connection.setRequestProperty("accept", "*/*");
            connection.setRequestProperty("connection", "Keep-Alive");
            connection.setRequestProperty("user-agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.114 Safari/537.36");
            //建立实际的链接
            connection.connect();
            //获取所有响应头字段
            Map<String, List<String>> map = connection.getHeaderFields();
            for (String key : map.keySet()) {
                System.out.println(key + "==========" + map.get(key));
            }

            in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String line;
            if ((line = in.readLine()) != null) {
                result += line;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return result;
    }


    /**
     * 发送POST请求
     *
     * @param url   请求url
     * @param param 请求参数，格式：name1=value1&name2=value2
     * @return 返回结果
     */
    public static String sendPost(String url, String param) {
        String result = "";
        PrintWriter out = null;
        BufferedReader in = null;
        try {
            URL realUrl = new URL(url);
            //打开一个连接
            URLConnection connection = realUrl.openConnection();
            //设置通用的请求属性
            connection.setRequestProperty("accept", "*/*");
            connection.setRequestProperty("connection", "Keep-Alive");
            connection.setRequestProperty("user-agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.114 Safari/537.36");
            //发送post请求必须设置如下两行
            connection.setDoOutput(true);
            connection.setDoInput(true);
            //获取URLConnection对象对应的输出流
            out = new PrintWriter(connection.getOutputStream());
            //发送请求参数
            out.println(param);
            out.flush();

            //定义BufferedReader输入流来读取URL的响应
            in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String line;
            if ((line = in.readLine()) != null) {
                result += line;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (out != null) {
                    out.close();
                }

                if (in != null) {
                    in.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

        }


        return result;
    }


    public static void main(String[] args) {
        String s = sendGet("http://localhost:8080/captchaImage", null);
        System.out.println(s);
    }


}
